﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using Maticsoft.Common;
using Model;

namespace Web.Admin.NavItem
{
    public partial class Item_Add : BaseCode.BaseAdmin
    {
        private DAL_Tags dalTags = new DAL_Tags();
        private string path = System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["titlexmlPath"]);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TagList();
            }
        }

        public void TagList()
        {
            DataTable list = dalTags.GetTagByType(1);
            for (int i = 0; i < list.Rows.Count; i++)
            {
                ListItem li = new ListItem();
                li.Value = list.Rows[i]["id"].ToString();
                li.Text = list.Rows[i]["Name"].ToString();
                li.Attributes.Add("ToopTip", "'" + list.Rows[i]["id"].ToString() + "'");
                this.cblTaglist.Items.Add(li);
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string strname = this.txtName.Text.Trim();
            string strurl = this.txtUrl.Text.Trim();

            string strtypes = "";
            for (int i = 0; i < this.cblTaglist.Items.Count; i++)
            {
                if (this.cblTaglist.Items[i].Selected == true)
                {
                    strtypes += this.cblTaglist.Items[i].Value + ",";
                }
            }
            strtypes = (strtypes.Trim().Length > 0)
                           ? strtypes.Trim().Substring(0, strtypes.Trim().Length - 1)
                           : strtypes;
            List<Title> list = XMLLib.getTitle(path);
            int id = 0;
            foreach (var title in list)
            {
                if (strname == title.TitleName)
                {
                    Page.ClientScript.RegisterStartupScript(typeof(string), "提示", "<script>alert('该导航已添加！');</script>");
                    return;
                }
                id = (int.Parse(title.TitleID) > id) ? int.Parse(title.TitleID) : id;
            }
            XMLLib.AddXML((id + 1).ToString(), strname, strurl, strtypes, path);
            Response.Redirect("Item.aspx");
        }
    }
}